package com.ml.springbootidus.cotroller;

import com.ml.springbootidus.mapper.UserInfoMapper;
import com.ml.springbootidus.pojo.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.List;

/**
 * 用户接口【Mybatis】
 */
@RestController
public class UserInfoController {

    @Autowired
    UserInfoMapper userInfoMapper;

    /**
     * 添加用户信息
     *
     * @param uname        用户名称
     * @param usex         用户性别
     * @param udescription 用户描述
     * @return
     */
    @PostMapping("/insertuser")
    public String insertUser(@RequestParam("uname") String uname,
                             @RequestParam("usex") String usex,
                             @RequestParam("udescription") String udescription) {
        UserInfo userInfo = new UserInfo();
        userInfo.setUname(uname);
        userInfo.setUsex(usex);
        userInfo.setUdescription(udescription);
        userInfo.setUstatus(1);
        Integer integer = userInfoMapper.insertUserInfo(userInfo);
        if (integer == 1) {
            return "已经添加：请访问【http://localhost:8080/listuser】";
        } else {
            return "添加失败";
        }
    }

    /**
     * 逻辑删除用户信息
     *
     * @param userid 用户ID
     * @return
     */
    @DeleteMapping("/deluser/{userid}")
    public String delUser(@PathVariable("userid") String userid) {
        UserInfo userInfo = new UserInfo();
        userInfo.setUstatus(0);
        userInfo.setUserid(userid);
        Integer integer = userInfoMapper.delUserInfo(userInfo);
        if (integer == 1) {
            return "已经删除：请访问【http://localhost:8080/listuser】";
        } else {
            return "删除失败";
        }
    }

    /**
     * 修改用户信息
     *
     * @param userid 用户ID
     * @param uname  用户姓名
     * @param usex   用户性别
     * @return
     */
    @PutMapping("/updateuser")
    public String updateUser(@RequestParam("userid") String userid,
                             @RequestParam("uname") String uname,
                             @RequestParam("usex") String usex) {
        UserInfo userInfo = new UserInfo();
        userInfo.setUserid(userid);
        userInfo.setUname(uname);
        userInfo.setUsex(usex);
        Integer integer = userInfoMapper.updateUserInfo(userInfo);
        if (integer == 1) {
            return "已经修改：请访问【http://localhost:8080/listuser】";
        } else {
            return "修改失败";
        }
    }

    /**
     * 获取用户信息
     *
     * @return
     */
    @GetMapping("/listuser")
    public List<UserInfo> listUser() {
        List<UserInfo> userInfos = userInfoMapper.getUserInfos();
        if (userInfos != null) {
            return userInfos;
        } else {
            return null;
        }
    }

}
